import chakankonggui.AvailableLockersPanel;
import complain.ComplaintPanel;
import cunjian.DepositPanel;
import guanli.AdminPanel;
import jijian.SendExpressPanel;
import kuaidi.AdminOperatePanel;
import login.RoleLoginDialog;
import qujian.PickupPanel;
import weixiu.RepairPanel;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {
    private JPanel mainPanel;
    private CardLayout cardLayout;

    public MainFrame() {
        setTitle("智能快递柜管理系统");
        setSize(1100, 650);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 顶部标题栏
        JLabel titleLabel = new JLabel("智能快递柜管理系统", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 28));
        titleLabel.setOpaque(true);
        titleLabel.setBackground(new Color(36, 49, 60));
        titleLabel.setForeground(Color.WHITE);
        titleLabel.setPreferredSize(new Dimension(0, 60));
        add(titleLabel, BorderLayout.NORTH);

        // 左侧菜单（用户）
        JPanel userPanel = createMenuPanel("用户功能", new String[]{
                "扫码存件", "取件", "寄快递服务", "查看空柜", "投诉异常", "退出系统"
        }, 180, 150);

        // 右侧菜单（物流：快递员/维修人员/管理员）
        JPanel logisticsPanel = createMenuPanel("物流功能", new String[]{
                "快递员操作模块", "维修人员模块", "管理员模块"
        }, 220, 200);

        // 主面板（卡片布局）
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // ✅ 欢迎界面使用缩放图片
        ImageIcon rawIcon = new ImageIcon("tupian/welcome.jpg"); // 图片路径
        Image scaledImage = rawIcon.getImage().getScaledInstance(900, 500, Image.SCALE_SMOOTH);
        ImageIcon scaledIcon = new ImageIcon(scaledImage);
        JLabel welcomeLabel = new JLabel(scaledIcon);
        welcomeLabel.setHorizontalAlignment(SwingConstants.CENTER);
        welcomeLabel.setVerticalAlignment(SwingConstants.CENTER);
        welcomeLabel.setPreferredSize(new Dimension(900, 500));
        mainPanel.add(welcomeLabel, "home");

        // 注册各功能模块
        mainPanel.add(new DepositPanel(), "deposit");
        mainPanel.add(new PickupPanel(), "pickup");
        mainPanel.add(new AvailableLockersPanel(), "viewAvailable");
        mainPanel.add(new ComplaintPanel(), "complaint");
        mainPanel.add(new SendExpressPanel(), "sendPackage");
        mainPanel.add(new AdminOperatePanel(), "adminOperate");
        mainPanel.add(new RepairPanel(), "repairPanel");
        mainPanel.add(new AdminPanel(), "adminPanel");

        // 添加到主窗口
        add(userPanel, BorderLayout.WEST);
        add(logisticsPanel, BorderLayout.EAST);
        add(mainPanel, BorderLayout.CENTER);

        setVisible(true);
    }

    // 创建功能面板（菜单）
    private JPanel createMenuPanel(String title, String[] items, int panelWidth, int buttonWidth) {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBackground(new Color(245, 245, 245));
        panel.setPreferredSize(new Dimension(panelWidth, 0));
        panel.setBorder(BorderFactory.createTitledBorder(title));

        for (String item : items) {
            JButton btn = createModernButton(item, buttonWidth);
            btn.setAlignmentX(Component.CENTER_ALIGNMENT);
            panel.add(Box.createVerticalStrut(15));
            panel.add(btn);

            btn.addActionListener(e -> handleMenuClick(item));
        }

        return panel;
    }

    // 自定义按钮样式
    private JButton createModernButton(String text, int width) {
        JButton button = new JButton(text);
        button.setFocusPainted(false);
        button.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        button.setBackground(new Color(100, 149, 237));
        button.setForeground(Color.WHITE);
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        Dimension size = new Dimension(width, 40);
        button.setPreferredSize(size);
        button.setMaximumSize(size);
        button.setMinimumSize(size);

        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(80, 120, 200)),
                BorderFactory.createEmptyBorder(8, 20, 8, 20)
        ));

        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(new Color(70, 130, 180));
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(new Color(100, 149, 237));
            }
        });

        return button;
    }

    // 功能点击处理
    private void handleMenuClick(String item) {
        switch (item) {
            case "扫码存件" -> cardLayout.show(mainPanel, "deposit");
            case "取件" -> cardLayout.show(mainPanel, "pickup");
            case "寄快递服务" -> cardLayout.show(mainPanel, "sendPackage");
            case "查看空柜" -> cardLayout.show(mainPanel, "viewAvailable");
            case "投诉异常" -> cardLayout.show(mainPanel, "complaint");

            case "快递员操作模块" -> {
                RoleLoginDialog dialog = new RoleLoginDialog(this, "快递员");
                dialog.setVisible(true);
                if (dialog.isLoginSuccessful()) {
                    cardLayout.show(mainPanel, "adminOperate");
                } else {
                    JOptionPane.showMessageDialog(this, "未授权，无法访问该功能。", "提示", JOptionPane.WARNING_MESSAGE);
                }
            }

            case "维修人员模块" -> {
                RoleLoginDialog dialog = new RoleLoginDialog(this, "维修人员");
                dialog.setVisible(true);
                if (dialog.isLoginSuccessful()) {
                    cardLayout.show(mainPanel, "repairPanel");
                } else {
                    JOptionPane.showMessageDialog(this, "未授权，无法访问该功能。", "提示", JOptionPane.WARNING_MESSAGE);
                }
            }

            case "管理员模块" -> {
                RoleLoginDialog dialog = new RoleLoginDialog(this, "管理员");
                dialog.setVisible(true);
                if (dialog.isLoginSuccessful()) {
                    cardLayout.show(mainPanel, "adminPanel");
                } else {
                    JOptionPane.showMessageDialog(this, "未授权，无法访问该功能。", "提示", JOptionPane.WARNING_MESSAGE);
                }
            }

            case "退出系统" -> System.exit(0);
            default -> JOptionPane.showMessageDialog(this, "未知功能：" + item);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(MainFrame::new);
    }
}
